package action;

import java.util.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import db.*;

@SuppressWarnings("serial")
public class RegisterUserAction extends ActionSupport {

	private User user;
	private UserDAO userDao;
	private String confirmPassword;
	
	
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
		this.user.setURole(0);
	}
	
	public void validate(){
		List users_username = userDao.findByUsername(this.user.getUsername());
		List users_email = userDao.findByUEmail(this.user.getUEmail());
		if (!confirmPassword.equals(this.user.getUDescription()))
			addFieldError("confirmPassword", getText("message.repeat.password"));
		if(users_username.size() > 0)
			addFieldError("account.username", getText("message.repeat.username")); 
		if(users_email.size() > 0)
			addFieldError("account.email", getText("message.repeat.eamil")); 			
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() {
		try{
			//
		}
		catch(Exception e){
			e.printStackTrace();
			return ERROR;
		}
		
		ActionContext ctx = ActionContext.getContext();  
        Map session = ctx.getSession(); 
        session.put("admin", "1");//administrator
        session.put("user", "0");//user (common)
        session.put("username", this.user.getUsername());
        
		return SUCCESS;
	}
}
 

